// pages/sugar_record/sugar_record.js
import config from '../../mars/conf/config'
let api = require("../../mars/services/api.js");
let that;
Page({
  data: {
      mealActive:"lunch",
      target:{
        breakfast:{
          mainFood: 230,
          milkAndEgg: 160,
          vagatable: 70,
          oil: 316
        },
        lunch: {
          mainFood: 250,
          milkAndEgg: 170,
          vagatable: 50,
          oil: 135
        },
        supper: {
          mainFood: 210,
          milkAndEgg: 110,
          vagatable: 50,
          oil: 135
        },
      },
      curTotalCalorie: {
        breakfast: {
          mainFood: 0,
          milkAndEgg: 0,
          vagatable: 0,
          oil:0
        },
        lunch: {
          mainFood: 0,
          milkAndEgg: 0,
          vagatable: 0,
          oil: 0
        },
        supper: {
          mainFood: 0,
          milkAndEgg: 0,
          vagatable: 0,
          oil: 0
        },
      },
      meal:{
         breakfast:{
            mainFood: [],
            milkAndEgg: [],
            vagatable: [],
            oil: []
         },
         lunch: {
           mainFood: [],
           milkAndEgg: [],
           vagatable: [],
           oil: []
         },
         supper: {
           mainFood: [],
           milkAndEgg: [],
           vagatable: [],
           oil: []
         }
      }
   },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
    let mealName = options.meal ||'breakfast';
    that.setData({
      mealActive: mealName
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    that.refreshData(that.data.mealActive);
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  refreshData(mealName){
    let mealData = wx.getStorageSync(mealName);
    let data = {};
    let nullMeal = {
      mainFood: [],
      milkAndEgg: [],
      vagatable: [],
      oil: []
    };
    that.data.meal[mealName] = mealData || nullMeal;
    data.meal = that.data.meal;
    data.mealActive = mealName;

    let totalEnergy = mealData;
    let totalCal = ['mainFood', 'milkAndEgg', 'vagatable', 'oil'].map(item => {
      var tmpColorie = 0;
      mealData[item].forEach(item => {
        tmpColorie += parseInt(item.kcal);
      });
      return tmpColorie;
    });

    that.data.curTotalCalorie[mealName] = {
      mainFood: totalCal[0],
      milkAndEgg: totalCal[1],
      vagatable: totalCal[2],
      oil: totalCal[3]
    }
    data.curTotalCalorie = that.data.curTotalCalorie;

    this.setData(data);
  },
  navigate:function(data){
    let foodType = data.currentTarget.dataset.foodtype;
    let target = that.data.target[that.data.mealActive][foodType];
    wx.navigateTo({
      url: '/pages/calorieFoodEdit/calorieFoodEdit?meal=' + this.data.mealActive + 
        "&foodType=" + foodType +
        "&target=" + target,
    });
  },
})



